<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Code;
use Illuminate\Support\Facades\Mail;

class SendEmails extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'emails:send {code_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '邮件任务.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $codeId = $this->argument('code_id');
        
        $code = $this->getCode($codeId);
        
        $subject = '【青苜网】面试通知提醒';
        
        if($code->type == 89) {
            $subject = '【青苜网】面试通知提醒';
        
            $data = json_decode($code->content);
        
            $content = [
                'user'=>$data->user,
                'need'=>$data->need,
                'company'=>$data->company,
                'url'=>$data->url,
                'iuser'=>$data->iuser,
                'iphone'=>$data->iphone,
                'address'=>$data->address
            ];
        
            $res = Mail::send('emails.notice',$content,function($message) use($subject,$code){
                $message->from(env('MAIL_USERNAME','master2@cimers.com'), '青苜网');
                $message ->to($code->phone)->subject($subject);
            });
        }
        if($res)
        {
            $code->update(['status'=>2]);
        }
        else
        {
            $code->update(['status'=>3]);
        }
        
    }
    
    /**
     * 获取code.
     *
     * @param Int $codeId
     */
    private function getCode($codeId)
    {
        $code = Code::find($codeId);
    
        return $code;
    }
}
